#include "attendanceruleform.h"
#include "ui_attendanceruleform.h"

attendanceruleForm::attendanceruleForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::attendanceruleForm)
{
    ui->setupUi(this);

    QProgressDialog *loadingDialog = new QProgressDialog("正在获取数据...", "取消", 0, 0, this);
    loadingDialog->setWindowTitle("请稍候");
    loadingDialog->setWindowModality(Qt::WindowModal);  // 模态窗口
    loadingDialog->show();

    QMap<QString, QString> params;
    params["rule_name"] = "标准工时制";

    HttpManager::instance()->get(
                QString(SERVER_BASE_URL) + "/api/attendance_rules/by_name",
                params,
                [this, loadingDialog](const QJsonObject& result) {
        // 数据获取成功，关闭加载弹窗并释放资源
        loadingDialog->close();
        delete loadingDialog;  // 关闭后直接释放

        if (result["success"].toBool()) {
            QJsonArray dataArray = result["data"].toArray();
            if (!dataArray.isEmpty()) {
                QJsonObject ruleData = dataArray[0].toObject();

                ui->timeEdit->setTime(QTime::fromString(ruleData["work_start"].toString(), "HH:mm:ss"));
                ui->timeEdit_2->setTime(QTime::fromString(ruleData["checkin_start"].toString(), "HH:mm:ss"));
                ui->timeEdit_3->setTime(QTime::fromString(ruleData["work_end"].toString(), "HH:mm:ss"));
                ui->timeEdit_4->setTime(QTime::fromString(ruleData["checkout_end"].toString(), "HH:mm:ss"));
                ui->timeEdit_5->setTime(QTime::fromString(ruleData["split_time"].toString(), "HH:mm:ss"));
            } else {
                QMessageBox::warning(this, "数据为空", "未查询到标准工时制规则数据");
            }
        } else {
            QMessageBox::warning(this, "错误", result["message"].toString());
        }
    },
    [this, loadingDialog](int code, const QString& msg) {
        // 数据获取失败，关闭加载弹窗并释放资源
        loadingDialog->close();
        delete loadingDialog;  // 关闭后直接释放

        QMessageBox::critical(this, "请求失败", QString("错误码: %1, 信息: %2").arg(code).arg(msg));
    }
    );
}

attendanceruleForm::~attendanceruleForm()
{
    delete ui;
}

void attendanceruleForm::on_pushButton_clicked()
{
    // 获取UI时间值
    QTime workStart = ui->timeEdit->time();
    QTime checkinStart = ui->timeEdit_2->time();
    QTime workEnd = ui->timeEdit_3->time();
    QTime checkoutEnd = ui->timeEdit_4->time();
    QTime split_time = ui->timeEdit_5->time();

    // 验证时间有效性
    if (!workStart.isValid() || !checkinStart.isValid() ||
            !workEnd.isValid() || !checkoutEnd.isValid()) {
        QMessageBox::warning(this, "输入错误", "请输入有效的时间");
        return;
    }

    // 显示加载对话框
    QProgressDialog *loadingDialog = new QProgressDialog("正在提交修改...", "取消", 0, 0, this);
    loadingDialog->setWindowTitle("请稍候");
    loadingDialog->setWindowModality(Qt::WindowModal);
    loadingDialog->show();

    // 构建请求数据
    QJsonObject jsonData;
    jsonData["rule_name"] = "标准工时制";
    jsonData["work_start"] = workStart.toString("HH:mm:ss");
    jsonData["checkin_start"] = checkinStart.toString("HH:mm:ss");
    jsonData["split_time"] = split_time.toString("HH:mm:ss");
    jsonData["work_end"] = workEnd.toString("HH:mm:ss");
    jsonData["checkout_end"] = checkoutEnd.toString("HH:mm:ss");

    HttpManager::instance()->postJson(
                QString(SERVER_BASE_URL) + "/api/attendance_rules/by_name",
                jsonData,
                [this, loadingDialog](const QJsonObject& result) {
        // 成功回调
        loadingDialog->close();
        delete loadingDialog;
        if (result["success"].toBool()) {
            QMessageBox::information(this, "成功", result["message"].toString());
        } else {
            QMessageBox::warning(this, "失败", result["error"].toString());
        }
    },
    [this, loadingDialog](int code, const QString& msg) {
        // 错误回调
        loadingDialog->close();
        delete loadingDialog;
        QMessageBox::critical(this, "请求失败", QString("错误码: %1, 信息: %2").arg(code).arg(msg));
    }
    );
}
